import requests
import json
from base64 import b64decode
from lxml import etree
from fontTools.ttLib import TTFont
import re


headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36"
}
cookies = {
    "sessionid": "j2i6vqj93aauttzk5kcxu5yc8e5xsuen"
}
url = "https://match.yuanrenxue.cn/api/match/7"
res_list = []
# 通过ttf在线编辑器，得到保存在本地的ttf字体对应编码(每次变化)  https://fonteditor.musp.cn/
def get_data_font(page):

    params = {
        "page": page
    }
    response = requests.get(url, headers=headers, cookies=cookies, params=params)
    # 网站返回数据
    res_json = response.json()['data']
    # 网站字体展示格式数据woff
    res_font = response.json()['woff']
    b_de = b64decode(res_font)
    with open(r'E:\Python\vscode\Crawlers\yuanrenxue\no7\task7.ttf', 'wb') as f:
        f.write(b_de)

    # ttf - > html
    font_obj = TTFont(r'E:\Python\vscode\Crawlers\yuanrenxue\no7\task7.ttf')
    font_obj.saveXML(r'E:\Python\vscode\Crawlers\yuanrenxue\no7\task7.xml')
    with open(r'E:\Python\vscode\Crawlers\yuanrenxue\no7\task7.xml', 'r', encoding='utf-8') as f:
        e = etree.XML(f.read().encode('utf-8'))
    # 读取 JSON 文件
    with open(r"E:\Python\vscode\Crawlers\yuanrenxue\no7\task7_a.json", "r", encoding="utf-8") as f:
        loaded_data = json.load(f)
    enum_key = {}
    for i in range(2,12):
        result_num = e.xpath(f'//glyf/TTGlyph[{i}]/contour/pt/@on')
        result_num = ''.join(result_num)
        result_name = e.xpath(f'//glyf/TTGlyph[{i}]/@name')
        enum_key[result_name[0]] = loaded_data[result_num]
    # 替换为字典中的值
    for number1 in res_json:
        res_num = number1['value'].replace('&#x', 'uni').strip().split(' ')
        mapped_values = [enum_key[key] for key in res_num]
        # 2. 合并为整数
        result = int("".join(map(str, mapped_values)))
        # print(enum_key)  {'unia579': 9, 'unia725': 0, 'unib269': 5, 'unib289': 7, 'unib932': 2, 'unib957': 4, 'unic478': 1, 'unic817': 3, 'unic863': 8, 'unic976': 6}
        res_list.append(result)

def dict_num():
    the_nums = {}
    d_num = {
        'unic725' : 0,
        'unia986' : 1,
        'unib279' : 2,
        'unia914' : 3,
        'unia561' : 4,
        'unia574' : 5,
        'unic563' : 6,
        'unia954' : 7,
        'unib472' : 8,
        'unic369' : 9
    }
    with open(r'E:\Python\vscode\Crawlers\yuanrenxue\no7\task7.xml', 'r', encoding='utf-8') as f:
        e = etree.XML(f.read().encode('utf-8'))
    for i in range(2,12):
        result_num = e.xpath(f'//glyf/TTGlyph[{i}]/contour/pt/@on')
        result_num = ''.join(result_num)
        result_name = e.xpath(f'//glyf/TTGlyph[{i}]/@name')
        the_nums[result_num] = d_num[result_name[0]]
    
    # 将字典根据value值的大小排序0->9
    the_nums = dict(sorted(the_nums.items(), key=lambda item: item[1]))
    print(the_nums)
    # 将数字对应关系保存在本地
    with open(r'E:\Python\vscode\Crawlers\yuanrenxue\no7\task7_a.json', 'w', encoding='utf-8') as f:
        json.dump(the_nums, f, indent=4, ensure_ascii=False)
# 只用作一次数据对应关系保存，数据存放在task7_a.json
# dict_num()

for i in range(1,6):
    get_data_font(i)
max_value = max(res_list)
for i, num in enumerate(res_list):
    if num == max_value:
        name_index = i
l_name = ['爷灬霸气傀儡', '梦战苍穹', '傲世哥', 'мaη肆風聲', '一刀メ隔世', '横刀メ绝杀', 'Q不死你R死你', '魔帝殤邪', '封刀不再战', '倾城孤狼', '戎马江湖', '狂得像风', '影之哀伤', '謸氕づ独尊', '傲视狂杀', '追风之梦', '枭雄在世', '傲视之巅', '黑夜刺客', '占你心为王', '爷来取你狗命', '御风踏血', '凫矢暮城', '孤影メ残刀', '野区霸王', '噬血啸月', '风逝无迹', '帅的睡不着', '血色杀戮者', '冷视天下', '帅出新高度', '風狆瑬蒗', '灵魂禁锢', 'ヤ地狱篮枫ゞ', '溅血メ破天', '剑尊メ杀戮', '塞外う飛龍', '哥‘K纯帅', '逆風祈雨', '恣意踏江山', '望断、天涯路', '地獄惡灵', '疯狂メ孽杀', '寂月灭影', '骚年霸称帝王', '狂杀メ无赦', '死灵的哀伤', '撩妹界扛把子', '霸刀☆藐视天下', '潇洒又能打', '狂卩龙灬巅丷峰', '羁旅天涯.', '南宫沐风', '风恋绝尘', '剑下孤魂', '一蓑烟雨', '领域★倾战', '威龙丶断魂神狙', '辉煌战绩', '屎来运赚', '伱、Bu够档次', '九音引魂箫', '骨子里的傲气', '霸海断长空', '没枪也很狂', '死魂★之灵'];
# 根据索引得到最后的名字
print(l_name[name_index])
